package com.zzptc.llq.bdsjws;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;

import com.socks.library.KLog;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zzptc.llq.bdsjws.db.DaoMaster;
import com.zzptc.llq.bdsjws.db.DaoSession;
import com.zzptc.llq.bdsjws.utils.MyUtils;
import com.zzptc.llq.bdsjws.vo.Box;
import com.zzptc.llq.bdsjws.com.Constants;
import com.zzptc.llq.bdsjws.db.BoxDao;
import com.zzptc.llq.bdsjws.vo.Contact;

import org.greenrobot.greendao.database.Database;
import com.zzptc.llq.bdsjws.R;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by zzsgz on 2017/4/20.
 */

public class MyApplication extends Application {
    //功能：1、提供全局的上下文 2、初始化KLOG
    static Context context;
    static DaoSession daoSession;
    static DaoSession mobiledaoSession;
   static List<Contact> list;
    static List<Contact> soslist=new ArrayList<Contact>();
    public static List<Contact> getSoslist(){
      //  soslist=new ArrayList<Contact>();
        return soslist;
    }

    public static List<Contact> getList() {
        return list;
    }

    //真正的入口方法
    @Override
    public void onCreate() {
        super.onCreate();
        inChlient();
        context=getApplicationContext();
        KLog.init(BuildConfig.LOG_DEBUG, Constants.LOG_FLAG);
        initDatebase();
        initDatebase1();
        //耗时操作放入现成
        new Thread(new Runnable() {
            @Override
            public void run() {
                initDatetoTable();
                //将外部数据库拷贝到手机存储器中
                try {
                    MyUtils.copyFileToMobile(context,"mobile.db","mobile.db");
                    list=MyUtils.getAllContact();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();

    }
    public void inChlient(){
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();
        OkHttpUtils.initClient(okHttpClient);
    }

    public static Context getContext(){

        return context;
    }
    //初始化GreenDao百宝箱的
    public static void initDatebase(){
       DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,Constants.DATEBASE_NAME);
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
      daoSession=daoMaster.newSession();
    }
    //初始化GreenDao外部数据库的
    public static void initDatebase1(){
        DaoMaster.DevOpenHelper openHelper=new DaoMaster.DevOpenHelper(context,"mobile.db");
        Database database=openHelper.getWritableDb();
        DaoMaster daoMaster=new DaoMaster(database);
        mobiledaoSession=daoMaster.newSession();
    }
    public static DaoSession getMobiledaoSession(){
        return mobiledaoSession;//外部数据库
    }
    public static DaoSession getDaoSession(){
        return daoSession;//百宝箱
    }
    //j将数据新增到表 要对表增删改查必须要获得BoxDao       对象只能操作一次  共享首选项
    public void initDatetoTable() {
        DaoSession daoSession = getDaoSession();
        BoxDao boxDao = daoSession.getBoxDao();
//        try {
//            MyUtils.copyFileToMobile(context,"mobile.db","aaa.db");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        SharedPreferences sharedPreferences = MyUtils.getpreferences();
        boolean b = sharedPreferences.getBoolean("insert_table", false);
        if (!b) {
            //数据源在String.xml中

            //读取String.xml中的文件
            String[] names = getResources().getStringArray(com.zzptc.llq.bdsjws.R.array.bbx_name);
            String[] icons = getResources().getStringArray(com.zzptc.llq.bdsjws.R.array.bbx_icon);
            for (int i = 0; i < names.length; i++) {
                Box box = new Box(null, names[i], icons[i]);

                    long m = boxDao.insert(box);
                    if (m > 0) {
                        KLog.i("新增成功");
                    } else {
                        KLog.i("新增失败");
                    }
            }


        }
        int len=boxDao.loadAll().size();
        KLog.i(len+"-------新增了几条记录-------------");
        for(Box box:boxDao.loadAll()){
            KLog.i(box.getName()+"===================="+box.getIcon());
        }
//新增完毕  将共享首选项的值改为true
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putBoolean("insert_table",true);
        editor.commit();

    }

}
